package com.zit.java.controller;


import com.zit.java.pojo.SysUser;
import com.zit.java.service.sysuser.UserSerivce;
import com.zit.java.util.DataUtils;
import com.zit.java.util.object.AjaxDone;
import com.zit.java.util.object.TableData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("user")
public class UserController extends BaseController {
    @Autowired
    private UserSerivce serivce;

    @GetMapping(value = "user/{id}")
    @ResponseBody
    public AjaxDone queryUser(@PathVariable("id") int id) {
        SysUser sysUser = serivce.queryUser(id);
        return new AjaxDone(200, "查询成功", sysUser);
    }

    @DeleteMapping("user/{uid}")
    @ResponseBody
    public AjaxDone delUser(@PathVariable("uid") int id) {
        int rst = serivce.delUser(id);
        if (rst > 0) {
            return new AjaxDone(200, "删除成功");
        }
        return new AjaxDone(300, "删除失败!");
    }

    @PostMapping("user/{uid}/{loginName}")
    @ResponseBody                                                                      //这个参数可以映射到对象属性
    public AjaxDone updateUser(@PathVariable("uid") int id, @PathVariable("loginName") String loginName, SysUser sysUser) {
        int rst = serivce.updateUser(id, sysUser);
        if (rst > 0) {
            return new AjaxDone(200, "修改成功");
        }
        return new AjaxDone(300, "修改失败!");
    }

    /**
     * 到用户列表
     *
     * @return
     */
    @RequestMapping("toUser")
    public String toUserList() {
        return "user/index";
    }


    /**
     * 显示表格数据
     *
     * @param name
     * @param loginName
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("showUsers")
    @ResponseBody
    public TableData<SysUser> selectUser(String name, String loginName, int page, int limit) {
        TableData<SysUser> tb = serivce.queryUserByCondition(name, loginName, page, limit);
        return tb;
    }

    /**
     * 到设置角色页面
     *
     * @param id
     * @return
     */
    @RequestMapping("setUserRole")
    public String toSetUserRole(@ModelAttribute("id") String id, Model model) {
        //同步方式
        List<Map<String, Object>> rolesList = serivce.queryAllRoles(id);
        //[{role_name=学生, ishave=1, role_id=40, role_status=1},
        // {role_name=教师, ishave=0, role_id=59, role_status=1},
        // {role_name=管理员, ishave=0, role_id=60, role_status=1}]
        //将List绑定到页面zhong
        model.addAttribute("roleList", rolesList);

        return "user/setRole";

    }

    /**
     * 保持设置用户角色的权限
     *
     * @return
     */

    @RequestMapping("saveSetRole")
    @ResponseBody
    public AjaxDone saveSetRole(@RequestParam(value = "roleIds[]", required = false) String[] roleIds, String id, HttpServletRequest request) {
        try {
            //id 是用户id
            int rst = serivce.updateUserRole(roleIds, id, request);
            if (rst > 0) {
                return new AjaxDone(200, "成功");
            }
        } catch (Exception e) {
            return new AjaxDone(300, "服务繁忙!" + e.getMessage());
        }
        return new AjaxDone(300, "服务繁忙!");
    }

    /**
     * 删除用户
     *
     * @param sysUser
     * @return
     */
    @RequestMapping("deleteUser")
    @ResponseBody
    public AjaxDone delUser(SysUser sysUser) {
        int rst = serivce.deleteUser(sysUser);
        if (rst > 0) {
            return new AjaxDone(200, "删除成功");
        }
        return new AjaxDone(300, "服务器繁忙");
    }

    /**
     * 到添加用界面
     *
     * @return
     */
    @RequestMapping("toAddUser")
    public String toAddUser() {
        return "user/addUser";
    }

    /**
     * 添加用户
     *
     * @param sysUser
     * @return
     */
    @RequestMapping("addUser")
    @ResponseBody
    public AjaxDone addUser(SysUser sysUser) {
    sysUser.setCreateDate(DataUtils.getCurrentTime());
        int rst = serivce.saveUser(sysUser);
        if (rst > 0) {
            return new AjaxDone(200, "添加成功");
        }
        return new AjaxDone(300, "服务繁忙");
    }


    /**
     * 修改用户信息
     * @param
     * @return
     */
    @RequestMapping("/updUser")
    @ResponseBody
    public AjaxDone updUser(SysUser sysUser){
       int res = serivce.updateUserDate(sysUser);
       if (res>0){
           return new AjaxDone(200,"更新成功");
       }
        return new AjaxDone(300,"更新失败");
    }

    @RequestMapping("batchDelete")
    @ResponseBody
    public AjaxDone batchDelete(@RequestParam("ids[]") List<String> ids) {

        int rst = serivce.batchDelUser(ids);
        if (rst > 0) {
            return new AjaxDone(200, "批量删除成功");
        }
        return new AjaxDone(300, "批量删除失败");
    }

    @RequestMapping("/queryByName")
    @ResponseBody
    public TableData<SysUser> queryByUserName(@Param("name") String name){
        if (name.equals(null)||"".equals(name)){
            TableData<SysUser> tableData = new TableData<>();
            tableData.setMsg("名字不能为空");
            return  tableData;
        }
       TableData<SysUser>  tableData  =  serivce.queryUserByName(name);
        return tableData;
    }
}
